import {  useRoute } from "vue-router";
import { computed } from "vue";
import { useStore } from "vuex";
export const useContent = () => {
  const store = useStore();
  const route = useRoute();
  const shopId = route.params.id;
  // 设置change为计算属性
  const change = computed(() => {
    const list = { content: {productList:{}}, price: 0 };
    // 设置list.content.shopName为当前购物车的商店名
    list.content.shopName=store.state.cartList?.[shopId].shopName;
    // 如果购物车中的商品被选中，则把该商品放入到list.content.porductList中
    for (let k in store.state.cartList?.[shopId].productList) {
      if (store.state.cartList[shopId].productList[k].check) {
        list.content.productList[k]=store.state.cartList[shopId].productList[k];
      }
    }
    // 设置list.price为每件商品数量与价格的积的累加
    for (let k in list.content.productList) {
      list.price +=
        list.content.productList[k].count * list.content.productList[k].price;
    }
    list.price = list.price.toFixed(2);
    console.log(list);
    return list;
  });
  // 导出change，shopId
  return { change,shopId };
};